package com.flying.invest.assets.spider;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;

import com.flying.common.annotation.Transaction;
import com.flying.common.http.HttpUtils;
import com.flying.common.http.ResponseHandler;
import com.flying.common.util.Codes;
import com.flying.common.util.DateUtils;
import com.flying.common.util.StringUtils;
import com.flying.common.util.Utils;
import com.flying.framework.annotation.Param;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.service.AbstractService;
import com.flying.invest.assets.model.Assets;
import com.flying.invest.assets.service.AssetsPriceService;
import com.flying.invest.assets.service.AssetsService;

public class NeteaseHistoryPriceSpider extends AbstractService implements IHistoryPriceSpider{
	private AssetsService assetsService;
	private AssetsPriceService priceService;
	
	public void setAssetsService(AssetsService s) {
		this.assetsService = s;
	}
	public void setAssetsPriceService(AssetsPriceService s) {
		this.priceService = s;
	}
	
	@Override
	@Transaction
	public ActionResult fetch(@Param("code")String code, @Param("from_date")Date from_date, @Param("to_date")Date to_date) throws Exception {
		var start = from_date == null? "20100101":DateUtils.formatDate(from_date, "yyyyMMdd");
		var end = to_date == null? DateUtils.formatDate(new Date(), "yyyyMMdd"):DateUtils.formatDate(to_date, "yyyyMMdd");
		Assets assets = this.assetsService.findByCode(code);
		var url = this.getUrl(assets.getCode(), start, end);
		System.out.println(">>>:"+url);
		final List<String> lines = Utils.newArrayList();
		HttpUtils.get(url, new ResponseHandler<Integer>() {
			@Override
			public Integer convert(InputStream is) throws Exception {
				try(var reader = new InputStreamReader(is, "GBK")){
					char[] buff = new char[1];
					String line = "";
					int readcount = reader.read(buff);
					while(readcount > 0) {
						if(buff[0] == '\n') {
							lines.add(line.trim());
							line = "";
						} else {
							line += buff[0];
						}
						readcount = reader.read(buff);
					}
				}
				return lines.size() - 1;
			}
		});
		//
		for(int i=1; i< lines.size(); i++) {
			String[] items = lines.get(i).split(",");
			String price_date = items[0];
			Double close_price = StringUtils.isEmpty(items[3])? null: Double.parseDouble(items[3]);
			Double max_price = StringUtils.isEmpty(items[4])? null: Double.parseDouble(items[4]);
			Double min_price = StringUtils.isEmpty(items[5])? null: Double.parseDouble(items[5]);
			Double open_price = StringUtils.isEmpty(items[6])? null: Double.parseDouble(items[6]);
			if(open_price * close_price * max_price * min_price > 0) {
				this.priceService.removeByDate(assets.getAssetsId(), price_date);
				this.priceService.create(assets.getAssetsId(), close_price, min_price, max_price, open_price, close_price, price_date, new Date());
			}
		}
		//
		return ModelFactory.create(ActionResult.class, Codes.CODE, Codes.SUCCESS, Codes.EFFECT_ROWS, lines.size() - 1);
	}

	public String getUrl(String code, String start, String end) {
		String market = this.getServiceConfig().getConfig("market");
		return "http://quotes.money.163.com/service/chddata.html?code="+market+code+"&start="+start+"&end="+end+"&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP";
	}
}
